import { defineStore } from 'pinia'

export const useArrivalStore = defineStore('arrival', {
  state: () => ({
    // 到货单列表
    arrivalList: [],
    // 扫描项列表
    scanItems: []
  }),
  
  getters: {
    getArrivalList: (state) => state.arrivalList,
    getScanItems: (state) => state.scanItems
  },
  
  actions: {
    /**
     * 设置到货单列表
     * @param {Array} list - 到货单列表数据
     */
    setArrivalList(list) {
      this.arrivalList = list
    },
    
    /**
     * 添加到货单
     * @param {Object} item - 到货单项
     */
    addArrivalItem(item) {
      this.arrivalList.push(item)
    },
    
    /**
     * 清空到货单列表
     */
    clearArrivalList() {
      this.arrivalList = []
    },
    
    /**
     * 设置扫描项列表
     * @param {Array} items - 扫描项列表
     */
    setScanItems(items) {
      this.scanItems = items
    },
    
    /**
     * 添加扫描项
     * @param {Object} item - 扫描项
     */
    addScanItem(item) {
      this.scanItems.push(item)
    },
    
    /**
     * 更新扫描项
     * @param {Object} item - 更新的扫描项
     */
    updateScanItem(item) {
      const index = this.scanItems.findIndex(scanItem => scanItem.id === item.id)
      if (index !== -1) {
        this.scanItems.splice(index, 1, item)
      }
    },
    
    /**
     * 删除扫描项
     * @param {String|Number} id - 扫描项ID
     */
    removeScanItem(id) {
      const index = this.scanItems.findIndex(item => item.id === id)
      if (index !== -1) {
        this.scanItems.splice(index, 1)
      }
    },
    
    /**
     * 清空扫描项列表
     */
    clearScanItems() {
      this.scanItems = []
    }
  }
})